<?php

namespace App\Http\Controllers\Backend;

use App\Http\Resources\Backend\BookOrderResource;
use App\Models\BookOrder;
use Illuminate\Http\Request;

/**
 * Class BookOrderController
 * @package App\Http\Controllers\Backend
 * 订单列表
 */
class BookOrderController extends BackendController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * 订单列表
     */
    public function index(Request $request)
    {
        $bookOrders = BookOrder::orderBy('created_at', 'desc')
            ->with('rule')->with('user.role')
            ->where('school_id', $request->school_id)
            ->where('book_rule_id', $request->rule_id)
            ->typeIs($request->orderType)
            ->statusIs($request->status)
            ->eatTypeIs($request->eat_type)
            ->eatDateIs($request->eat_date)
            ->eatNameLike($request->eat_name)
            ->eatPhoneIs($request->eat_phone)
            ->calculatedIs($request->is_calculated)
            ->priceRuleIs($request->price_rule);
        if ($request->type === 'export') {
            $data = $bookOrders->get();
        } else {
            $data = $bookOrders->paginate($request->size);
        }
        return BookOrderResource::collection($data);
    }
}
